package com.teamsun.system;

import com.teamsun.datasource.DynamicDataSource;
import com.teamsun.entity.UserInfo;
import com.teamsun.exception.ServiceException;
import com.teamsun.mapper.LoginMapper;
import com.teamsun.mapper.LoginMySQLMapper;
import com.teamsun.util.Constants;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 用户登录服务
 */
public class LoginServiceImpl implements ILoginService {

    @Autowired
    private LoginMapper loginMapper;

    @Autowired
    private LoginMySQLMapper loginMySQLMapper;

    @Autowired
    private DynamicDataSource dynamicDataSource;

    /**
     * 用户登录验证
     * @param userId 用户名
     * @param password 密码
     * @return
     */
    @Override
    public UserInfo verifyLoginInfo(String userId, String password) throws ServiceException {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("userId", userId);
        List<UserInfo> users = loginMapper.queryUsrInfo(params);
        if(!CollectionUtils.isEmpty(users)) {
            UserInfo userInfo = users.get(0);
            String userPasswd = userInfo.getPassword();
            if(!password.equals(userPasswd)) {
                throw new ServiceException("密码错误");
            }
            return userInfo;
        } else {
            throw new ServiceException("用户不存在");
        }
    }

    /**
     * 根据用户ID查询用户信息
     * @param userId
     * @return
     * @throws ServiceException
     */
    @Override
    public List<UserInfo> queryUserById(String userId) throws Exception {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("userId", userId);
        List<UserInfo> users = loginMapper.queryUsrInfo(params);
        return users;
    }

    /**
     * 分页查询用户信息
     * @param param 查询条件
     * @param startIndex 起始数据
     * @param endIndex 结束数据
     * @return
     */
    @Override
    public Map<String, Object> queryUserInfo(Map<String, Object> param, int startIndex, int endIndex) {
        Map<String, Object> resultMap = new HashMap<String, Object>();

        // 分页查询用户信息
        param.put("endIndex", endIndex);
        param.put("startIndex", startIndex);
        String userName = (String) param.get("userName");
        if(StringUtils.isNotBlank(userName)) {
            userName = "%" + userName + "%";
            param.put("userName", userName);
        }

        String dbType = dynamicDataSource.getMasterDBType();
        List<Map<String, Object>> userInfos = new ArrayList<Map<String, Object>>();
        int userInfoCount = 0;
        if(Constants.DBTYPE_MYSQL.equalsIgnoreCase(dbType)) {
            int offset = startIndex - 1;
            int rows = endIndex - offset;
            param.put("offset", offset);
            param.put("rows", rows);

            userInfos = loginMySQLMapper.queryUsrInfoByPage(param);
            // 查询用户数
            userInfoCount = loginMySQLMapper.queryUsrInfoCount(param);
        } else {
            userInfos = loginMapper.queryUsrInfoByPage(param);

            // 查询用户数
            userInfoCount = loginMapper.queryUsrInfoCount(param);
        }

        resultMap.put("rows", userInfos);
        resultMap.put("total", userInfoCount);
        return resultMap;
    }

    /**
     * 删除用户信息
     * @param params  限制条件
     * @throws Exception
     */
    @Override
    public void deleteUserInfo(Map<String, Object> params) throws Exception{
        loginMapper.deleteUserInfo(params);
    }

    /**
     * 新增用户信息
     * @param userInfo
     * @throws Exception
     */
    @Override
    public void addUserInfo(UserInfo userInfo) throws Exception {
        String dbType = dynamicDataSource.getMasterDBType();
        if(Constants.DBTYPE_MYSQL.equalsIgnoreCase(dbType)) {
            loginMySQLMapper.addUserInfo(userInfo);
        } else {
            loginMapper.addUserInfo(userInfo);
        }
    }

    /**
     * 修改用户信息
     * @param userInfo
     * @throws Exception
     */
    @Override
    public void updateUserInfo(UserInfo userInfo) throws Exception {
        String dbType = dynamicDataSource.getMasterDBType();
        if(Constants.DBTYPE_MYSQL.equalsIgnoreCase(dbType)) {
            loginMySQLMapper.updatUserInfo(userInfo);
        } else {
            loginMapper.updatUserInfo(userInfo);
        }
    }

    /**
     * 修改用户密码
     * @param userInfo
     * @throws Exception
     */
    @Override
    public void updateUserPassword(UserInfo userInfo) throws Exception {
        loginMapper.updatPassword(userInfo);
    }
}
